package cn.bjxy.javaks.store_ms.ui;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import cn.bjxy.javaks.store_ms.entity.AdminEntity;
import cn.bjxy.javaks.store_ms.service.AdminServiceImpl;
import cn.bjxy.javaks.store_ms.service.IAdminService;
import cn.bjxy.javaks.store_ms.service.LogInvocationHandler;

import javax.swing.JPasswordField;
import java.awt.Font;
import javax.swing.JButton;

public class DengLuFrame {

	private JFrame frame;
	private JTextField textField;
	private JPasswordField passwordField;

	public static void main(String[] args) {
		new DengLuFrame();
	}

	/**
	 * 登录界面构造器
	 */
	public DengLuFrame() {
		initialize();
	}

	/**
	 * 初始化登录界面
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 550, 345);
		frame.setLocationRelativeTo(null);// 居中显示
		frame.setResizable(false);
		frame.setTitle("登录界面");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(new CardLayout(0, 0));

		JPanel background = new JPanel();
		background.setBackground(new Color(25, 51, 64));
		frame.getContentPane().add(background, "name_117535879739679");
		background.setLayout(null);

		JPanel middle = new JPanel();
		middle.setBounds(176, 96, 150, 100);
		middle.setBackground(new Color(25, 51, 64));
		background.add(middle, BorderLayout.CENTER);
		middle.setLayout(new GridLayout(4, 1, 0, 0));

		JLabel account = new JLabel("帐号");
		account.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
		account.setForeground(Color.WHITE);
		middle.add(account);

		textField = new JTextField();
		textField.setForeground(Color.WHITE);
		textField.setOpaque(false);
		textField.setCaretColor(Color.WHITE);
		middle.add(textField);
		textField.setColumns(10);

		JLabel password = new JLabel("密码");
		password.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
		password.setForeground(Color.WHITE);
		middle.add(password);

		passwordField = new JPasswordField();
		passwordField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
		passwordField.setOpaque(false);
		passwordField.setForeground(Color.WHITE);
		passwordField.setCaretColor(Color.WHITE);
		middle.add(passwordField);

		JButton button = new JButton("登录");
		button.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 14));
		button.setForeground(Color.WHITE);
		button.setBounds(176, 206, 150, 23);
		button.setBackground(new Color(25, 51, 64));
		button.setFocusable(false);
		background.add(button);

		frame.setVisible(true);

		button.addMouseListener(new MouseListener());
		textField.addKeyListener(new KeyListener());
		passwordField.addKeyListener(new KeyListener());
	}

	/**
	 * 判断登录是否成功
	 */
	private void isTrue() {
		IAdminService adminService = AdminServiceImpl.getProxyInstance();

		/* 获得帐号和密码 */
		String account = textField.getText();
		String passWord = passwordField.getText();

		AdminEntity adminEntity = adminService.findByNameAndPass(account, passWord);
		if (adminEntity != null) {
			/* 登录成功 */
			LogInvocationHandler.setUser(account);
			if (adminEntity.getLevel() == 0) {
				frame.dispose();
				MainFrame.setAdminEntity(adminEntity);
				new MainFrame();
				return;
			} else {
				frame.dispose();
				MyFrame.setAdminEntity(adminEntity);
				new MyFrame();
				return;
			}
		} else {
			/* 登录失败 弹出提示框 */
			JOptionPane.showMessageDialog(new JFrame().getContentPane(), "用户名不存在或密码错误!\n请重新输入！", "系统信息",
					JOptionPane.ERROR_MESSAGE);
		}
	}

	class KeyListener extends KeyAdapter {
		@Override
		public void keyPressed(KeyEvent e) {
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				isTrue();
			}
		}
	}

	class MouseListener extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			isTrue();
		}
	}
}
